import { Dispatch } from '../types/dispatch'
import { MiddlewareAPI, MiddleWare } from '../types/middleware'
import { Reducer } from '../types/reducers'

// export function applyMiddleware<State>(
//   ...middlewares: MiddleWare<State>[]
// ): StoreEnhancer<any> {
//   return createStore => (reducer, preloadedState) => {
//     const store = createStore(reducer, preloadedState)
//     let dispatch: Dispatch = () => {
//       throw new Error(
//         'Dispatching while constructing your middleware is not allowed. ' +
//           'Other middleware would not be applied to this dispatch.'
//       )
//     }
//
//     const middlewareAPI: MiddlewareAPI = {
//       getState: store.getState,
//       dispatch: (action, ...args) => dispatch(action, ...args)
//     }
//     const chain = middlewares.map(middleware => middleware(middlewareAPI))
//     dispatch = compose<typeof dispatch>(...chain)(store.dispatch)
//
//     return {
//       ...store,
//       dispatch
//     }
//   }
// }